package com.vcredit.creditcat.service.loginPreview;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.model.PersonInfo;
import com.vcredit.creditcat.service.PersonInfoService;
import com.vcredit.creditcat.vo.CheckBlackParamVo;
import com.vcredit.creditcat.vo.product.sd.SDQueryParamVo;
import com.vcredit.creditcat.vo.product.sd.SDQueryResultVo;
import com.vcredit.creditcat.vo.product.sd.SDResultVo;
import com.vcredit.framework.exception.BaseException;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 闪贷登录预检处理类
 * Created by liaoshengjun on 2017/11/9.
 */
@Service("SDPreviewService")
public class SDPreviewService implements IProductPreview{
	@Value("${product.sd.checkStatusUrl}")
	private String checkStatusUrl;
	//贷前黑名单检查
	@Value("${check.black.url}")
	private String checkBlackUrl;
	@Autowired
	private PersonInfoService personInfoService;

	private static Logger logger = LoggerFactory.getLogger(SDPreviewService.class);
	@Override
	public Integer checkExist(String mobile) throws Exception {
		if(StringUtils.isBlank(mobile)){
			throw new BaseException("调用闪贷登录预检失败：手机号为空");
		}
		SDResultVo<SDQueryResultVo> result  = null;
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("custMobile", mobile);
			String resultJson = HttpRequest.doJson(checkStatusUrl, JacksonUtil.Obj2Json(map));
			logger.info("闪贷登录预检接口返回："+ resultJson+"手机号:"+mobile);
			// 转成实体对象
			result = JacksonUtil.Json2Obj(resultJson, new TypeReference<SDResultVo<SDQueryResultVo>>(){});
		} catch (Exception e) {
			logger.error("闪贷登录预检接口调用失败"+"手机号:"+mobile,e);
			throw e;
		}
		if("1".equals(result.getStatus()) && result.getData() != null) {
			Integer orderState = result.getData().getOrderState();
			if(orderState!=null && orderState.equals(0)) {
				if(checkBlack(mobile)){
					return 1;
				}else{
					return 0;
				}
			}else{
				return 0;
			}
		}else{
			throw new BaseException("调用闪贷登录预检失败"+"手机号:"+mobile);
		}
	}
	private boolean checkBlack(String mobile)throws Exception {
		boolean result;
		PersonInfo personParam=new PersonInfo();
		personParam.setPersonPhone(mobile);
		List<PersonInfo> personInfoList = personInfoService.find(personParam);
		if(personInfoList ==null || personInfoList.size()==0){
			return true;
		}
		CheckBlackParamVo checkBlackParamVo=new CheckBlackParamVo();
		checkBlackParamVo.setProjectTeam("信用猫");
		checkBlackParamVo.setProductName("LOANKIND/DOUDOUHUA");
		checkBlackParamVo.setIdentityNo(personInfoList.get(0).getPersonIdeno());
		checkBlackParamVo.setName(personInfoList.get(0).getPersonName());
		checkBlackParamVo.setAction("300");
		checkBlackParamVo.setMobile(mobile);
		try{
			String resultJson =HttpRequest.doJson(checkBlackUrl, JacksonUtil.Obj2Json(checkBlackParamVo));
			logger.info("闪贷登录黑名单检查接口返回："+ resultJson+"手机号:"+mobile);
			Map<String,Object> resultMap=JacksonUtil.Json2Map(resultJson);
			if(resultMap.get("Status")!=null ){
				Integer status=(Integer)resultMap.get("Status");
				if(status.equals(1)){
					Map<String,Object> contentMap=JacksonUtil.Json2Map(resultMap.get("Content").toString());
					Integer black=(Integer)contentMap.get("IsBlackUser");
					if(black.equals(1)){
						result=false;
					}else{
						result=true;
					}
				}else if(status.equals(0)){
					result=true;
				}else{
					throw new BaseException("闪贷登录黑名单检查接口调用异常"+"手机号:"+mobile);
				}
			}else{
				throw new BaseException("闪贷登录黑名单检查接口调用异常"+"手机号:"+mobile);
			}
			return result;
		}catch (Exception e){
			logger.error("闪贷登录黑名单检查接口调用失败"+"手机号:"+mobile,e);
			throw e;
		}
	}
}
